/*******************************************************************************
 *                                    ZLG
 *                         ----------------------------
 *                         innovating embedded platform
 *
 * Copyright (c) 2001-present Guangzhou ZHIYUAN Electronics Co., Ltd.
 * All rights reserved.
 *
 * Contact information:
 * web site:    https://www.zlg.cn
 *******************************************************************************/
#ifndef __HPM6E00_REGS_USB_H
#define __HPM6E00_REGS_USB_H

#ifdef __cplusplus
extern "C" {
#endif  /* __cplusplus*/

#include "core/include/hpm6e00_regs_base.h"
#include <stdint.h>


#define USB_DCCPARAMS_DC_POS             (7U)              /* USB 控制器设备控制器位 */
#define USB_DCCPARAMS_DC_MASK            (0x1U << USB_DCCPARAMS_DC_POS)
#define USB_DCCPARAMS_DEN_POS            (0U)              /* USB 控制器设备控制器设备端点数量 */
#define USB_DCCPARAMS_DEN_MASK           (0x1F << USB_DCCPARAMS_DEN_POS)

#define USB_USBCMD_ITC_POS               (16U)
#define USB_USBCMD_ITC_MASK              (0xFFU << USB_USBCMD_ITC_POS)
#define USB_USBCMD_ATDTW_POS             (14U)
#define USB_USBCMD_ATDTW_MASK            (0x1U << USB_USBCMD_ATDTW_POS)
#define USB_USBCMD_SUTW_POS              (13U)
#define USB_USBCMD_SUTW_MASK             (0x1U << USB_USBCMD_SUTW_POS)
#define USB_USBCMD_RST_POS               (1U)              /* USB 控制器复位位*/
#define USB_USBCMD_RST_MASK              (0x1U << USB_USBCMD_RST_POS)
#define USB_USBCMD_RS_POS                (0U)              /* USB 控制器运行停止位*/
#define USB_USBCMD_RS_MASK               (0x1U << USB_USBCMD_RS_POS)

/* \brief USB 状态寄存器位定义*/
#define USB_USBSTS_SLI_POS               (8U)              /* 设备控制器挂起中断*/
#define USB_USBSTS_SLI_MASK              (0x1U << USB_USBSTS_SLI_POS)
#define USB_USBSTS_SRI_POS               (7U)              /* SOF 接收中断*/
#define USB_USBSTS_SRI_MASK              (0x1U << USB_USBSTS_SRI_POS)
#define USB_USBSTS_URI_POS               (6U)              /* USB 复位中断*/
#define USB_USBSTS_URI_MASK              (0x1U << USB_USBSTS_URI_POS)
#define USB_USBSTS_SEI_POS               (4U)              /* USB 帧列表滚动中断*/
#define USB_USBSTS_SEI_MASK              (0x1U << USB_USBSTS_SEI_POS)
#define USB_USBSTS_PCI_POS               (2U)              /* 端口变化检测中断*/
#define USB_USBSTS_PCI_MASK              (0x1U << USB_USBSTS_PCI_POS)
#define USB_USBSTS_UEI_POS               (1U)              /* USN 错误中断*/
#define USB_USBSTS_UEI_MASK              (0x1U << USB_USBSTS_UEI_POS)
#define USB_USBSTS_UI_POS                (0U)              /* USB 中断位*/
#define USB_USBSTS_UI_MASK               (0x1U << USB_USBSTS_UI_POS)

/* \brief USB 中断寄存器位定义*/
#define USB_USBINTR_SLE_POS              (8U)              /* USB 休眠中断使能位*/
#define USB_USBINTR_SLE_MASK             (0x1U << USB_USBINTR_SLE_POS)
#define USB_USBINTR_SRE_POS              (7U)              /* USB SOF 接收中断使能位*/
#define USB_USBINTR_SRE_MASK             (0x1U << USB_USBINTR_SRE_POS)
#define USB_USBINTR_URE_POS              (6U)              /* USB SOF 复位中断使能位*/
#define USB_USBINTR_URE_MASK             (0x1U << USB_USBINTR_URE_POS)
#define USB_USBINTR_AAE_POS              (5U)              /* USB 异步推进中断使能位*/
#define USB_USBINTR_AAE_MASK             (0x1U << USB_USBINTR_AAE_POS)
#define USB_USBINTR_SEE_POS              (4U)              /* USB 系统错误中断使能位*/
#define USB_USBINTR_SEE_MASK             (0x1U << USB_USBINTR_SEE_POS)
#define USB_USBINTR_FRE_POS              (3U)              /* USB 帧列表滚动中断使能位*/
#define USB_USBINTR_FRE_MASK             (0x1U << USB_USBINTR_FRE_POS)
#define USB_USBINTR_PCE_POS              (2U)              /* USB 端口变化检测中断使能位*/
#define USB_USBINTR_PCE_MASK             (0x1U << USB_USBINTR_PCE_POS)
#define USB_USBINTR_UEE_POS              (1U)              /* USB 错误中断使能位*/
#define USB_USBINTR_UEE_MASK             (0x1U << USB_USBINTR_UEE_POS)
#define USB_USBINTR_UE_POS               (0U)              /* USB 中断使能位*/
#define USB_USBINTR_UE_MASK              (0x1U << USB_USBINTR_UE_POS)

#define USB_DEVICEADDR_ADR_POS           (25U)
#define USB_DEVICEADDR_ADR_MASK          (0x3F << USB_DEVICEADDR_ADR_POS)
#define USB_DEVICEADDR_ADRA_POS          (24U)
#define USB_DEVICEADDR_ADRA_MASK         (0x1U << USB_DEVICEADDR_ADRA_POS)

#define USB_ENDPTLISRADDR_EPBASE_POS     (11U)             /* 端点链表指针*/
#define USB_ENDPTLISRADDR_EPBASE_MASK    (0x1FFFFFU << USB_ENDPTLISRADDR_EPBASE_POS)

#define USB_PORTSC1_STS_POS              (29U)             /* USB 串行平行收发器选择*/
#define USB_PORTSC1_STS_MASK             (0x1U << USB_PORTSC1_STS_POS)
#define USB_PORTSC1_PTW_POS              (28U)             /* 平行收发器宽度*/
#define USB_PORTSC1_PTW_MASK             (0x1U << USB_PORTSC1_PTW_POS)
#define USB_PORTSC1_PSPD_POS             (26U)             /* 端口速度位*/
#define USB_PORTSC1_PSPD_MASK            (0x3U << USB_PORTSC1_PSPD_POS)
#define USB_PORTSC1_PR_POS               (8U)              /* 端口复位*/
#define USB_PORTSC1_PR_MASK              (0x1U << USB_PORTSC1_PR_POS)
#define USB_PORTSC1_PE_POS               (2U)
#define USB_PORTSC1_PE_MASK              (0x1U << USB_PORTSC1_PE_POS)

#define USB_USBMODE_SLOM_POS             (3U)              /* SETUP 锁定 */
#define USB_USBMODE_SLOM_MASK            (0x1U << USB_USBMODE_SLOM_POS)
#define USB_USBMODE_ES_POS               (2U)              /* 大小端选择 */
#define USB_USBMODE_ES_MASK              (0x1U << USB_USBMODE_ES_POS)
#define USB_USBMODE_CM_POS               (0U)              /* USB 控制器模式 */
#define USB_USBMODE_CM_MASK              (0x3U << USB_USBMODE_CM_POS)

#define USB_ENDPTSETUPSTAT_POS           (0U)
#define USB_ENDPTSETUPSTAT_MASK          (0xFFFF << USB_ENDPTSETUPSTAT_POS)

/* \brief USB 端点控制寄存器位定义*/
#define USB_ENDPTCTRL_TXE_POS            (23U)             /* 端点发送使能*/
#define USB_ENDPTCTRL_TXE_MASK           (0x1U << USB_ENDPTCTRL_TXE_POS)
#define USB_ENDPTCTRL_TXR_POS            (22U)             /* 发送数据翻转复位*/
#define USB_ENDPTCTRL_TXR_MASK           (0x1U << USB_ENDPTCTRL_TXR_POS)
#define USB_ENDPTCTRL_TXT_POS            (18U)             /* 发送端点类型*/
#define USB_ENDPTCTRL_TXT_MASK           (0x3U << USB_ENDPTCTRL_TXT_POS)
#define USB_ENDPTCTRL_TXS_POS            (16U)             /* 发送端点停止*/
#define USB_ENDPTCTRL_TXS_MASK           (0x1U << USB_ENDPTCTRL_TXS_POS)
#define USB_ENDPTCTRL_RXE_POS            (7U)              /* 端点接收使能*/
#define USB_ENDPTCTRL_RXE_MASK           (0x1U << USB_ENDPTCTRL_RXE_POS)
#define USB_ENDPTCTRL_RXR_POS            (6U)              /* 接收数据翻转复位*/
#define USB_ENDPTCTRL_RXR_MASK           (0x1U << USB_ENDPTCTRL_RXR_POS)
#define USB_ENDPTCTRL_RXT_POS            (2U)              /* 接收端点类型*/
#define USB_ENDPTCTRL_RXT_MASK           (0x3U << USB_ENDPTCTRL_RXT_POS)
#define USB_ENDPTCTRL_RXS_POS            (0U)             /* 接收端点停止*/
#define USB_ENDPTCTRL_RXS_MASK           (0x1U << USB_ENDPTCTRL_RXS_POS)

#define USB_OTG_CTRL0_OTG_UTMI_SUSPENDM_SW_POS  (12U)
#define USB_OTG_CTRL0_OTG_UTMI_SUSPENDM_SW_MASK (0x1U << USB_OTG_CTRL0_OTG_UTMI_SUSPENDM_SW_POS)
#define USB_OTG_CTRL0_OTG_UTMI_RESET_SW_POS     (11U)
#define USB_OTG_CTRL0_OTG_UTMI_RESET_SW_MASK    (0x1U << USB_OTG_CTRL0_OTG_UTMI_RESET_SW_POS)

#define USB_OTG_CTRL1_OTG_DEBUG_SEL_POS         (0U)
#define USB_OTG_CTRL1_OTG_DEBUG_SEL_MASK        (0xFU << USB_OTG_CTRL1_OTG_DEBUG_SEL_POS)

#define USB_OTGSC_ASVIS_POS                     (18U)
#define USB_OTGSC_ASVIS_MASK                    (1U << USB_OTGSC_ASVIS_POS)
#define USB_OTGSC_AVVIS_POS                     (17U)
#define USB_OTGSC_AVVIS_MASK                    (1U << USB_OTGSC_AVVIS_POS)
#define USB_OTGSC_VD_POS                        (0U)
#define USB_OTGSC_VD_MASK                       (1U << USB_OTGSC_VD_POS)

#define USB_PHY_CTRL1_UTMI_CFG_RST_N_POS        (20U)
#define USB_PHY_CTRL1_UTMI_CFG_RST_N_MASK       (0x1U << USB_PHY_CTRL1_UTMI_CFG_RST_N_POS)
#define USB_PHY_CTRL1_UTMI_OTG_SUSPENDM_POS     (1U)
#define USB_PHY_CTRL1_UTMI_OTG_SUSPENDM_MASK    (0x1U << USB_PHY_CTRL1_UTMI_OTG_SUSPENDM_POS)

#define USB_PHY_STATUS_UTMI_CLK_VALID_POS       (31U)
#define USB_PHY_STATUS_UTMI_CLK_VALID_MASK      (0x1U << USB_PHY_STATUS_UTMI_CLK_VALID_POS)

/* \brief HPM USB 寄存器定义 */
typedef struct {
    volatile const uint32_t     id;                    /* ID 寄存器，偏移：0x0 */
    volatile const uint32_t     general;               /* 硬件通用寄存器，偏移：0x4 */
    volatile const uint32_t     host;                  /* 主机硬件参数寄存器，偏移：0x8 */
    volatile const uint32_t     device;                /* 设备硬件参数寄存器，偏移：0xC */
    volatile const uint32_t     txbuf;                 /* 发送缓存硬件参数寄存器，偏移：0x10 */
    volatile const uint32_t     rxbuf;                 /* 接收缓存硬件参数寄存器，偏移：0x14 */
    volatile const uint32_t     reserved1[26];         /* 0x18 - 0x7F：保留 */
    volatile uint32_t           gptimer0ld;            /* 通用定时器 0 加载寄存器，偏移：0x80 */
    volatile uint32_t           gptimer0ctrl;          /* 通用定时器 0 控制寄存器，偏移：0x84 */
    volatile uint32_t           gptimer1ld;            /* 通用定时器 1 加载寄存器，偏移：0x88 */
    volatile uint32_t           gptimer1ctrl;          /* 通用定时器 1 控制寄存器，偏移：0x8C */
    volatile uint32_t           sbuscfg;               /* 系统总线配置寄存器，偏移：0x90 */
    volatile const uint32_t     reserved2[27];         /* 0x94 - 0xFF：保留 */
    volatile const uint8_t      caplength;             /* 性能寄存器长度寄存器，偏移：0x100 */
    volatile const uint8_t      reserved3;             /* 0x101：保留 */
    volatile const uint16_t     hci_version;           /* 主机控制器接口版本寄存器，偏移：0x102 */
    volatile const uint32_t     hcs_params;            /* 主机控制器结构参数寄存器，偏移：0x104 */
    volatile const uint32_t     hcc_params;            /* 主机控制器性能参数寄存器，偏移：0x108 */
    volatile const uint32_t     reserved4[5];          /* 0x10C - 0x11F：保留 */
    volatile const uint16_t     dciversion;            /* 设备控制器接口版本寄存器，偏移：0x120 */
    volatile const uint8_t      reserved5[2];          /* 0x122 - 0x123：保留 */
    volatile const uint32_t     dcc_params;            /* 设备控制器性能参数寄存器，偏移：0x124 */
    volatile const uint32_t     reserved6[6];          /* 0x128 - 0x13F：保留 */
    volatile uint32_t           usb_cmd;               /* USB 命令寄存器，偏移：0x140 */
    volatile uint32_t           usb_sts;               /* USB 状态寄存器，偏移：0x144 */
    volatile uint32_t           usb_intr;              /* USB 中断使能寄存器，偏移：0x148 */
    volatile uint32_t           frindex;               /* USB 帧索引寄存器，偏移：0x14C */
    volatile const uint32_t     reserved7;             /* 0x150 - 0x153：保留 */
    union {
        volatile uint32_t       device_addr;           /* 设备地址寄存器，偏移：0x154 */
        volatile uint32_t       periodic_list_base;    /* 帧列表基地址寄存器，偏移：0x154 */
    };
    union {
        volatile uint32_t       endpoint_list_addr;    /* 端点列表地址寄存器，偏移：0x158 */
        volatile uint32_t       async_list_addr;       /* 异步调度地址寄存器，偏移：0x158 */
    };
    volatile const uint32_t     reserved8;             /* 0x15C - 0x15F：保留 */
    volatile uint32_t           burst_size;
    volatile uint32_t           tx_filltuning;
    volatile const uint32_t     reserved9[4];          /* 0x168 - 0x177：保留 */
    volatile uint32_t           endpt_nak;             /* 端点 NAK 寄存器，偏移：0x178 */
    volatile uint32_t           endpt_nak_en;          /* 端点 NAK 使能寄存器，偏移：0x17C */
    volatile const uint32_t     configflag;            /* 配置标志寄存器，偏移：0x180 */
    volatile uint32_t           portsc1;               /* 端口状态和配置寄存器，偏移：0x184 */
    volatile const uint32_t     reserved10[7];         /* 0x188 - 0x1A3：保留 */
    volatile uint32_t           otgsc;                 /* On-The-Go 状态及控制寄存器寄存器，偏移：0x1A4 */
    volatile uint32_t           usbmode;               /* USB 设备模式寄存器，偏移：0x1A8 */
    volatile uint32_t           endptsetupstat;        /* 端点 Setup 状态寄存器，偏移：0x1AC */
    volatile uint32_t           endpointprime;
    volatile uint32_t           endptflush;            /* 端点冲刷寄存器，偏移：0x1B4 */
    volatile const uint32_t     endptstatus;           /* 端点状态寄存器，偏移：0x1B8 */
    volatile uint32_t           endptcomplete;         /* 端点完成寄存器，偏移：0x1BC */
    volatile uint32_t           endptctrl[8];          /* 端点控制寄存器 0...7，偏移：0x1C0 - 0x1DC */
    volatile const uint32_t     reserved11[8];         /* 0x1E0 - 0x1FF：保留 */
    volatile uint32_t           otg_ctrl0;
    volatile uint32_t           otg_ctrl1;
    volatile const uint32_t     reserved12[2];
    volatile uint32_t           phy_ctrl0;
    volatile uint32_t           phy_ctrl1;
    volatile const uint32_t     reserved13[2];
    volatile uint32_t           top_status;
    volatile uint32_t           phy_status;
} hpm_usb_reg_t;

#define HPM_USB0   ((hpm_usb_reg_t *)HPM_USB0_BASE)

#ifdef __cplusplus
}
#endif  /* __cplusplus  */
#endif


